package com.isfto.service.impl;

import com.isfto.dao.ApueDao;
import com.isfto.dao.BeforeFlightChangeEnterAanOutDao;
import com.isfto.dao.FlightInfoDao;
import com.isfto.entity.tablepojo.*;
import com.isfto.service.FlightInfoService;
import com.isfto.until.TimeUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FlightInfoServiceImpl implements FlightInfoService {
    @Resource
    FlightInfoDao flightInfoDao;

    @Resource
    BeforeFlightChangeEnterAanOutDao flightChangeEnterAanOutDao;

    @Resource
    ApueDao apueDao;

    @Override
    public PageBean FlightInfoAll(Integer page, Integer pageSize) {
        Integer total = flightInfoDao.total();
        Integer  start=(page-1)*pageSize;
        List<FlightInfo> flightInfos = flightInfoDao.flightInfoAll(start, pageSize);
        for (int i = 0; i < flightInfos.size(); i++) {
            //如果判断为真,则航班延迟，修改航班开始时间，将状态改为1
            if(!flightInfos.get(i).getAbst().equals("")){
                List<Dlye> dlyes = flightInfoDao.frttInfo(flightInfos.get(i).getFide());
                if(flightInfos.get(i).getAbst().equals("DLY")){
                    if(dlyes.size()!=0){
                        //如果dlyes不为空说明集合有值，获取集合第一个数值
                        //航班延误原因
                        String abrs = flightInfos.get(i).getAbrs();
                        System.out.println(flightInfos.get(i));
                        ABRS cnnm= flightInfoDao.abrsInfo(abrs);
                        //航班原始时间
                        String startfptt = flightInfos.get(i).getStartfptt();
                        //航班延误时间
                        String fett =dlyes.get(0).getFrtt();
                        if(fett.equals("")){
                            fett=startfptt;
                        }
                        //修改起飞时间
                        flightInfos.get(i).setStartfptt(fett);
                        //修改到达时间
                        String endfplt = flightInfos.get(i).getEndfplt();
                        String dateEnd = TimeUtil.getDateEnd(startfptt,fett,endfplt);
                        flightInfos.get(i).setEndfplt(dateEnd);
                        //修改状态
                        flightInfos.get(i).setFlag(1);
                        //获取开始时间和修改后时间
                        String dateString=startfptt;
                        String dateString2=fett;
                        String datePoor = TimeUtil.getDatePoor(dateString, dateString2);
                        String substring = datePoor.substring(0, 1);
                        String minute=datePoor.substring(3,5);
                        String date = substring.equals("0") ? minute : datePoor;
                        //输出的提示信息
                        flightInfos.get(i).setAbrs("大约晚点"+date+"("+cnnm.getCnnm()+")");
                        //修改航班三字到岗编码
                        String endapcd = flightInfos.get(i).getEndapcd();
                        Apue cnnmed= apueDao.cnnm(endapcd);
                        if(cnnmed!=null){
                            flightInfos.get(i).setEndapcd(cnnmed.getFlightCnnm()+"("+endapcd+")");
                        }
                    }else{
                        String endapcd = flightInfos.get(i).getEndapcd();
                        Apue cnnm = apueDao.cnnm(endapcd);
                        if(cnnm!=null){
                            flightInfos.get(i).setEndapcd(cnnm.getFlightCnnm()+"("+endapcd+")");
                        }
                        flightInfos.get(i).setAbrs("准点起飞");
                    }
                }else{
                    if(dlyes.size()!=0){
                        //如果dlyes不为空说明集合有值，获取集合第一个数值
                        //航班延误原因
                        String abrs = flightInfos.get(i).getAbrs();
                        ABRS cnnm= flightInfoDao.abrsInfo(abrs);
                        //航班原始时间
                        String cfno = flightInfos.get(i).getCfno();
                        String fide = flightInfos.get(i).getFide();
                        String flid = flightChangeEnterAanOutDao.flid(fide);
                        FlightChange startfptt1 = flightChangeEnterAanOutDao.startfptt(cfno,flid);
                        String startfptt =startfptt1.getStartfptt();
                        //航班延误时间
                        String fett =flightInfos.get(i).getStartfptt();
                        if(fett.equals("")){
                            fett=startfptt;
                        }
                        //修改到达时间
                       /* String endfplt = flightInfos.get(i).getEndfplt();
                        //原始开始时间，修改之后开始时间，原始结束时间
                        String dateEnd = TimeUtil.getDateEnd(startfptt,fett,endfplt);
                        flightInfos.get(i).setEndfplt(dateEnd);*/
                        //修改状态
                        flightInfos.get(i).setFlag(1);
                        //获取开始时间和修改后时间
                        String dateString=startfptt;
                        String dateString2=fett;
                        String datePoor = TimeUtil.getDatePoor(dateString, dateString2);
                        String substring = datePoor.substring(0, 1);
                        String minute=datePoor.substring(3,5);
                        String date = substring.equals("0") ? minute : datePoor;
                        //输出的提示信息
                        flightInfos.get(i).setAbrs("大约晚点"+date+"("+cnnm.getCnnm()+")");
                        //修改航班三字到岗编码
                        String endapcd = flightInfos.get(i).getEndapcd();
                        Apue  cnnmed= apueDao.cnnm(endapcd);
                        if(cnnmed!=null){
                            flightInfos.get(i).setEndapcd(cnnmed.getFlightCnnm()+"("+endapcd+")");
                        }
                    }else{
                        String endapcd = flightInfos.get(i).getEndapcd();
                        Apue cnnm = apueDao.cnnm(endapcd);
                        if(cnnm!=null){
                            flightInfos.get(i).setEndapcd(cnnm.getFlightCnnm()+"("+endapcd+")");
                        }
                        flightInfos.get(i).setAbrs("准点起飞");
                    }
                }

            }else{
                String endapcd = flightInfos.get(i).getEndapcd();
                Apue cnnm = apueDao.cnnm(endapcd);
                if(cnnm!=null){
                    flightInfos.get(i).setEndapcd(cnnm.getFlightCnnm()+"("+endapcd+")");
                }
                flightInfos.get(i).setAbrs("准点起飞");
            }
        }
        List<FlightInfo> newFlightInfo=new ArrayList<>();
        for (int i = 0; i < flightInfos.size(); i++) {
            boolean flag=false;
            int newFlightInfoindex=0;
            int flightinfos=0;
            if(newFlightInfo.size()==0){
                newFlightInfo.add(flightInfos.get(i));
            }else{
                for (int i1 = 0; i1 < newFlightInfo.size(); i1++) {
                    if(newFlightInfo.get(i1).getCfno().contains(flightInfos.get(i).getCfno())){
                        String fcdp = flightInfos.get(i).getFcdp();
                        flightInfos.get(i).setFcdp("右"+fcdp);
                        newFlightInfo.add(flightInfos.get(i));
                        flag=true;
                        break;
                    }
                }
                if(!flag){
                    newFlightInfo.add(flightInfos.get(i));
                }
            }
        }
        PageBean pageBean=new PageBean(total,newFlightInfo);
        return pageBean;
    }

    @Override
    public List<AirLine> enterAirLine() {
        List<AirLine> airLines = flightInfoDao.enterAirLine();
        for (int i = 0; i < airLines.size(); i++) {
            String name=airLines.get(i).getName();
            Apue cnnm = apueDao.cnnm(name);
            if(cnnm!=null) {
                airLines.get(i).setName(cnnm.getFlightCnnm());
            }
        }
        return airLines;
    }

    /**
     * 出港航线信息查询
     * @return
     */
    @Override
    public List <AirLine> outAirLine() {
        List<AirLine> airLines = flightInfoDao.outAirLine();
        for (int i = 0; i < airLines.size(); i++) {
            String name=airLines.get(i).getName();
            Apue cnnm = apueDao.cnnm(name);
            if(cnnm!=null) {
                airLines.get(i).setName(cnnm.getFlightCnnm());
            }
        }
        return airLines;
    }

    @Override
    public AirLine countAirLine() {
        ArrayList<String> startlist=new ArrayList();
        ArrayList<String> endlist=new ArrayList();
        String str="";
        String endstr="";
        for(int i=1;i<=10;i++){
            if(i>9){
                str="20170610000000";
            }else {
                str = "2017060"+i+ "000000";
            }
            startlist.add(str);
        }
        for(int i=1;i<=10;i++){
            if(i>9){
                endstr="20170610235959";
            }else {
                endstr = "2017060"+i+ "235959";
            }
            endlist.add(endstr);
        }
        List<Integer> countinter=new ArrayList<>();
        List<Integer> countout=new ArrayList<>();
        AirLine airLine=new AirLine();
        for (int i = 0; i < startlist.size(); i++) {
            List<AirLine> airLinesinter=flightInfoDao.countAirLine(startlist.get(i), endlist.get(i),"2403");
            countinter.add(airLinesinter.get(0).getCount());
            List<AirLine> airLinesout=flightInfoDao.countAirLine(startlist.get(i), endlist.get(i),"2401");
            countout.add(airLinesout.get(0).getCount());
        }
        airLine.setCountinter(countinter);
        airLine.setCountout(countout);
        return airLine;
    }

    @Override
    public AirLine outcountAirLine() {
        ArrayList<String> startlist=new ArrayList();
        ArrayList<String> endlist=new ArrayList();
        String str="";
        String endstr="";
        for(int i=1;i<=10;i++){
            if(i>9){
                str="20170610000000";
            }else {
                str = "2017060"+i+ "000000";
            }
            startlist.add(str);
        }
        for(int i=1;i<=10;i++){
            if(i>9){
                endstr="20170610235959";
            }else {
                endstr = "2017060"+i+ "235959";
            }
            endlist.add(endstr);
        }
        List<Integer> countinter=new ArrayList<>();
        List<Integer> countout=new ArrayList<>();
        AirLine airLine=new AirLine();
        for (int i = 0; i < startlist.size(); i++) {
            List<AirLine> airLinesinter=flightInfoDao.outcountAirLine(startlist.get(i), endlist.get(i),"2403");
            countinter.add(airLinesinter.get(0).getCount());
            List<AirLine> airLinesout=flightInfoDao.outcountAirLine(startlist.get(i), endlist.get(i),"2401");
            countout.add(airLinesout.get(0).getCount());
        }
        airLine.setCountinter(countinter);
        airLine.setCountout(countout);
        return airLine;
    }

    @Override
    public Integer updateInfoStart(String upstartfptt, String startapcd, String bfstartfptt) {
        return flightInfoDao.updateInfoStart(upstartfptt,startapcd,bfstartfptt);
    }

    @Override
    public Integer updateInfoEnd(String upendfplt, String endapcd, String bfendfplt) {
        return flightInfoDao.updateInfoEnd(upendfplt, endapcd, bfendfplt);
    }

    @Override
    public PageBean flightInfoEnter(Integer page, Integer pageSize,String startapat) {
        Integer total = flightInfoDao.flightInfoEnterCount(startapat);
        List<FlightInfoDetail> flightInfoDetails = flightInfoDao.flightInfoEnter(page, pageSize,startapat);
        flightInfoDetails = flightConver(flightInfoDetails);
        PageBean pageBean=new PageBean(total,flightInfoDetails);
        System.out.println(pageBean);
        return pageBean;
    }

    @Override
    public PageBean flightInfoOut(Integer page, Integer pageSize,String endapat) {
        Integer total = flightInfoDao.flightInfoOutCount(endapat);
        List<FlightInfoDetail> flightInfoDetails = flightInfoDao.flightInfoOut(page, pageSize,endapat);
        flightInfoDetails= flightConver(flightInfoDetails);
        PageBean pageBean=new PageBean(total,flightInfoDetails);
        System.out.println(pageBean);
        return pageBean;
    }


    @Override
    public PageBean flightInfoDetailEnter(String startapcd, String endapcd,String startfptt,String endfplt,Integer page,Integer pageSize) {
        Apue startName = apueDao.startName(startapcd);
        Apue endName = apueDao.endName(endapcd);
        Integer total = flightInfoDao.flightCount(startName.getFlightCode(),endName.getFlightCode(),startfptt,endfplt);
        Integer  start=(page-1)*pageSize;
        List<FlightInfoDetail> flightInfoDetail = flightInfoDao.flightInfoDetailEnter(startName.getFlightCode(), endName.getFlightCode(),startfptt,endfplt,start,pageSize);
        flightInfoDetail = flightConver(flightInfoDetail);
        PageBean pageBean=new PageBean(total,flightInfoDetail);
        System.out.println(pageBean);
        return pageBean;
    }

    @Override
    public PageBean flightInfoDetailOut(String startapcd, String endapcd, String startfptt, String endfplt, Integer page, Integer pageSize) {
        Apue startName = apueDao.startName(startapcd);
        Apue endName = apueDao.endName(endapcd);
        Integer total = flightInfoDao.flightOutCount(startName.getFlightCode(),endName.getFlightCode(),startfptt,endfplt);
        Integer  start=(page-1)*pageSize;
        List<FlightInfoDetail> flightInfoDetail = flightInfoDao.flightInfoDetailOut(startName.getFlightCode(), endName.getFlightCode(),startfptt,endfplt,start,pageSize);
        flightInfoDetail = flightConver(flightInfoDetail);
        PageBean pageBean=new PageBean(total,flightInfoDetail);
        System.out.println(pageBean);
        return pageBean;
    }

    @Override
    public void bytickets(Tickets tickets) {
        flightInfoDao.bytickets(tickets);
    }

    @Override
    public List<Tickets> getstickets() {
       List<Tickets> list= flightInfoDao.gettickets();
       return list;
    }

    @Override
    public Boolean refundTicket(Integer id,Integer status) {
        Boolean b = flightInfoDao.refundTicket( id,status );
        System.out.println(b);
        return b;
    }

    public List<FlightInfoDetail> flightConver(List<FlightInfoDetail> flightInfoDetails){
        for (int i = 0; i < flightInfoDetails.size(); i++) {
            String startapcd = flightInfoDetails.get(i).getStartapcd();
            String endapcd=flightInfoDetails.get(i).getEndapcd();
            Apue cnnm = apueDao.cnnm(startapcd);
            Apue endcnnm=apueDao.cnnm(endapcd);
            if(cnnm!=null){
                flightInfoDetails.get(i).setStartapcd(cnnm.getFlightCnnm());
            }
            if(endcnnm!=null){
                flightInfoDetails.get(i).setEndapcd(endcnnm.getFlightCnnm());
            }
        }
        return flightInfoDetails;
    }
    
}
